package com.hrpmar.controller;

import com.hrpmar.controller.dto.PurplanDto;
import com.hrpmar.pojo.Druginfo;
import com.hrpmar.pojo.Purdetails;
import com.hrpmar.pojo.Purplan;
import com.hrpmar.service.PurplanService;
import com.hrpmar.utils.Pager;
import com.hrpmar.utils.genNumUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by c on 2017-11-17.
 */
@RestController
@RequestMapping("/pur")
public class PurplanRestController {

    @Resource
    private PurplanService purplanService;

    @RequestMapping("/search")
    public Pager search(Integer offset, Integer limit, PurplanDto condition){
        Pager pager = purplanService.search(offset,limit,condition);
        return pager;
    }

    @RequestMapping("/searchDrug")
    public List<Druginfo> searchDrug(){
        return purplanService.searchAllDrug();
    }

    @PostMapping("/getSelect")
    public Map<String,List> getSelect(){
        return purplanService.searchProAndWorker();
    }

    @PostMapping("/getDrugStock")
    public String getDrugStock(String drugNum){
        int stock=0;
        try{
            stock = purplanService.searchOneDrugStock(drugNum);
        }catch (RuntimeException e){
            stock=0;
        }
        return String.valueOf(stock);
    }

    @PostMapping("/searchPurNum")
    public String getPurNum(){
        return genNumUtil.getNum(purplanService.searchNewPurNum());
    }

    @PostMapping("/addDetails")
    public String addPurMap(Purdetails purdetails, HttpServletRequest req){
        return purplanService.addDels2Map(purdetails,req);
    }

    @PostMapping("/addPurplan")
    public String AddPurdetails(HttpServletRequest req, Purplan purplan){
        String flag = "false";
        try{
            purplanService.addPurplan(req,purplan);
            flag="true";
        }catch(RuntimeException e){
            flag="false";
        }
        return flag;
    }

    @PostMapping("/updateDetails")
    public Purdetails updateDelMap(HttpServletRequest req,Integer mapKey,Float delBuyprice,Float delRetailprice,Float delOneprice,Integer delBuynum){
       Purdetails purdetails=null;
        try{
           HttpSession sessions = req.getSession();
           Map<Integer,Purdetails> map = (Map<Integer,Purdetails>) sessions.getAttribute("purDelMap");
           Purdetails pur = map.get(mapKey);
           //修改
           pur.setDelBuyprice(delBuyprice);
           pur.setDelRetailprice(delRetailprice);
           pur.setDelOneprice(delOneprice);
           pur.setDelBuynum(delBuynum);
            purdetails =pur;
       }catch (RuntimeException e){

       }
        return purdetails;
    }

    @PostMapping("/delDels")
    public String delDels(HttpServletRequest req,Integer key){
        String flag = "false";
        try{
            HttpSession sessions = req.getSession();
            Map<Integer,Purdetails> map = (Map<Integer,Purdetails>) sessions.getAttribute("purDelMap");
            map.remove(key);
            flag="true";
        }catch (RuntimeException e){
            flag="false";
        }
        return flag;
    }

    @PostMapping("/getPurdetails")
    public Purdetails getPurdetails(Integer delId){
        return purplanService.getPurdetails(delId);
    }

    @PostMapping("/updels")
    public String updateDels(Purdetails dels){
        String flag = "false";
        try{
            purplanService.updateDels(dels);
            flag="true";
        }catch(RuntimeException e){
            flag="false";
        }
        return flag;
    }

    @PostMapping("/deldelsByid")
    public String delDelsBydelId(Integer delId){
        String flag = "false";
        try{
            purplanService.delDels(delId);
            flag="true";
        }catch(RuntimeException e){
            flag="false";
        }
        return flag;
    }


}
